package com.shop.service;

import com.shop.mapper.AuctionCustomerMapper;
import com.shop.mapper.AuctionMapper;
import com.shop.mapper.AuctionrecordMapper;
import com.shop.mapper.UserMapper;
import com.shop.pojo.*;
import com.shop.utils.AuctionPriceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AuctionServiceImpl implements AuctionService {
    @Autowired
    private AuctionMapper auctionMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private AuctionCustomerMapper auctionCustomerMapper;


    @Autowired
    private AuctionrecordMapper auctionrecordMapper;
    /*@Override
    public List<Auction> queryAllAuctions() {


        AuctionExample example = new AuctionExample();

        List<Auction> auctionList = this.auctionMapper.selectByExample(example);
        return auctionList;
    }*/

    @Override
    public List<Auction> queryAllAuctions(Auction auction, Integer pageNum) {
        Boolean flag = true;

        redisTemplate.setStringSerializer(new StringRedisSerializer());

        AuctionExample example = new AuctionExample();
        AuctionExample.Criteria criteria = example.createCriteria();


        // 判断 用户传入的多个条件或者没有传条件
        if (auction != null) {
            // 竞拍商品的名称
            if (auction.getAuctionname() != null && !"".equals(auction.getAuctionname())) {
                // 模糊查询
                criteria.andAuctionnameLike("%" + auction.getAuctionname() + "%");
                flag = false;
            }
            // 竞拍品描述的查询
            if (auction.getAuctiondesc() != null && !"".equals(auction.getAuctiondesc())) {
                // 匹配查询
                criteria.andAuctiondescEqualTo(auction.getAuctiondesc());
                flag = false;
            }
            // 大于开始时间
            if (auction.getAuctionstarttime() != null) {
                criteria.andAuctionstarttimeGreaterThan(auction.getAuctionstarttime());
                flag = false;
            }

            // 小于结束时间
            if (auction.getAuctionendtime() != null) {
                criteria.andAuctionendtimeLessThan(auction.getAuctionendtime());
                flag = false;
            }

            // 大于起拍价
            if (auction.getAuctionstartprice() != null) {
                criteria.andAuctionstartpriceGreaterThan(auction.getAuctionstartprice());
                flag = false;
            }
        }

        // 设置起拍时间的降序

        example.setOrderByClause("auctionstarttime desc");

        //String key = pageNum + "redis:" + auction.getAuctionname() + auction.getAuctiondesc() + auction.getAuctionstarttime() + auction.getAuctionendtime() + auction.getAuctionstartprice();


        if (flag){
            List<Auction> auctionList = (List<Auction>) this.redisTemplate.opsForValue().get(pageNum);
            if (auctionList == null) {
                auctionList = this.auctionMapper.selectByExample(example);
                this.redisTemplate.opsForValue().set(pageNum, auctionList);
                System.out.println("第一次走数据库");
                return auctionList;
            }
            System.out.println("走缓存");
            return auctionList;
        }


        List<Auction> auctionList = this.auctionMapper.selectByExample(example);
        return auctionList;
    }

    @Override
    public Auction selectAuctionAndAuctionRecordList(Integer auctionId) {

        Auction auction = this.auctionCustomerMapper.selectAuctionAndAuctionRecordList(auctionId);

        return auction;
    }


    /**
     * 竞价业务规则:
     * 1.判断结束时间，不能过期  (如果你提交的时间比 竞拍活动结束时间 晚，那么给出提示：当前竞拍活动已经结束(竞拍时间不能晚于结束时间))
     * 2.判断价格：
     * -- 如果商品从未竞价，价格必须高于起拍价
     * -- 后续的竞价，价格必须高于所有竞价的最高价
     *
     * @param auctionrecord
     */

    @Override
    public void saveAuctionRecord(Auctionrecord auctionrecord) throws Exception {

        Auction auction = this.auctionCustomerMapper.selectAuctionAndAuctionRecordList(auctionrecord.getAuctionid());
//        if(auction.getAuctionendtime().after(new Date())==false)
        if (auction.getAuctionendtime().after(new Date()) == false) {
            throw new AuctionPriceException("竞拍时间已过，无法竞拍");
        }

        if (auction.getAuctionrecodList().size() > 0) {
            if (auctionrecord.getAuctionprice().compareTo(auction.getAuctionrecodList().get(0).getAuctionprice()) < 1) {
                throw new AuctionPriceException("价格必须高于所有竞价的最高价");
            }
        } else {
            if (auctionrecord.getAuctionprice().compareTo(auction.getAuctionstartprice()) < 1) {
                throw new AuctionPriceException("必须高于起拍价");
            }
        }

        this.auctionrecordMapper.insert(auctionrecord);


    }



    @Override
    public List<AuctionCustomer> selectAuctionendtime() {

        //redisTemplate.setStringSerializer(new StringRedisSerializer());

        return this.auctionCustomerMapper.selectAuctionendtime();
    }

    @Override
    public List<Auction> selectAuctionNoendtime() {
        return this.auctionCustomerMapper.selectAuctionNoendtime();
    }

    @Override
    public void addAuction(Auction auction) {

        this.auctionMapper.insert(auction);
    }

    @Override
    public Integer deleteAuctionById(Integer id,Integer pageNum) {
//        TODO 是否删除竞拍记录表的对应信息？
//        AuctionrecordExample example = new AuctionrecordExample();
//        AuctionrecordExample.Criteria criteria = example.createCriteria();
//        criteria.andAuctionidEqualTo(id);
//        this.auctionrecordMapper.deleteByExample(example);

        this.redisTemplate.delete(pageNum);


        return this.auctionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Auction selectAuctionByAuctionID(Integer id) {

        Auction auction = this.auctionMapper.selectByPrimaryKey(id);

        return auction;
    }

    @Override
    public void updateAuctionById(Auction auction) {

        this.auctionMapper.updateByPrimaryKey(auction);


    }
}
